package darknet.core.impl.net;

import darknet.core.spec.net.Message;
import darknet.core.spec.net.MessageHeader;
import darknet.core.spec.net.Node;

/**
 * Node use messages to communicate.
 * 
 * @author yoyo
 */
@SuppressWarnings("serial")
public abstract class AbstractMessage implements Message {
    
    /** From */
    private Node from;
    
    /** To */
    private Node to;

    /** Message header */
    private MessageHeader header;
    
    /**
     * Intialise an abstract message.
     *
     * @param header The header
     * @param from The writer
     * @param to   The receiver
     */
    protected AbstractMessage(MessageHeader header, Node from, Node to) {

        if (header == null) {
            throw new IllegalArgumentException("header is null");
        }
        this.header = header;
	
	if (from == null) {
	    throw new IllegalArgumentException("from is null");
	}
	this.from = from;
	
	if (to == null) {
	    throw new IllegalArgumentException("to is null");
	}
	this.to = to;
    }

    @Override
    public MessageHeader getHeader() {
        return header;
    }
    
    @Override
    public Node getFrom() {
	return from;
    }
    
    @Override
    public Node getTo() {
	return to;
    }

    @Override
    public String toString() {

        StringBuilder builder = new StringBuilder("AbstractMessage[");
        builder.append("header=");
        builder.append(header);
        builder.append(", ");
        builder.append("from=");
        builder.append(from);
        builder.append(", ");
        builder.append("to=");
        builder.append(to);
        builder.append("]");

        return builder.toString();
    }
}
